<?php
    if (!isset($_COOKIE["cur_user"]) or !isset($_GET["courseID"])) {
        echo <<<EOF
<script>
alert("You need to log in!");
url="index.php";
window.location.href=url;
</script>
EOF;
    } else {
        setcookie("cur_user", $_COOKIE["cur_user"], time() + 3600);
        setcookie("cur_sid", $_COOKIE["cur_sid"], time() + 3600);
        date_default_timezone_set("PRC");
    }
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Course Information</title>
        <link rel="stylesheet" href="bootstrap_min.css">
        <style>
            .desc-box {
                border-radius: 10px;
                background-color: #d0e7e5;
            }

            #no-result-warning {
                font-family: Sansation, sans-serif;
                color: gray;
            }

            .hide {
                display: none;
            }
        </style>
    </head>
    <body>

        <nav class="navbar navbar-expand-md navbar-dark bg-dark">
            <div class="container-fluid">
                <a class="navbar-brand" href="#">
                    <span>Registration</span>
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                        data-bs-target="#navbarSupportedContent"
                        aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                        <li class="nav-item">
                            <a class="nav-link" href="stuCourseAvailable.php">Course Available</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="stuCourseSelected.php">Course Selected</a>
                        </li>
                        <li class="nav-item">
                            <a href="#" class="nav-link active">Course Information</a>
                        </li>
                    </ul>
                    <form action="logOut.php" method="post" class="d-flex">
                        <button class="btn btn-outline-danger" type="submit">Log Out</button>
                    </form>
                </div>
            </div>
        </nav>

        <section>
            <div class="container">
                <div class="row align-items-center">
                    <div class="col-md-3">
                        <?php
                            $pics = array(
                                "0" => "C.svg",
                                "1" => "linear.png",
                                "2" => "Lua.svg",
                                "3" => "database.svg",
                                "4" => "data_structure.svg",
                                "5" => "piano.jpg",
                                "6" => "film_score.jfif",
                                "7" => "C++.svg",
                                "8" => "algorithm.svg",
                                "9" => "php.svg",
                            );

                            $cid = $_GET["courseID"];
                            echo <<<EOF
<img style="border-radius: 16%" src="img/$pics[$cid]" class="img-fluid p-2" alt="icon">
EOF;
                        ?>
                    </div>
                    <div class="col-md-9">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>CID</th>
                                    <th>Course Name</th>
                                    <th>Course Time</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    $cid = $_GET["courseID"];
                                    $week_name = array(
                                        "1" => "Monday",
                                        "2" => "Tuesday",
                                        "3" => "Wednesday",
                                        "4" => "Thursday",
                                        "5" => "Friday",
                                        "6" => "Saturday",
                                        "7" => "Sunday",
                                    );

                                    $period_name = array(
                                        "1" => "8:00 - 9:35",
                                        "2" => "9:55 - 11:30",
                                        "3" => "13:20 - 14:55",
                                        "4" => "15:15 - 16:50",
                                        "5" => "18:30 - 20:55",
                                    );

                                    $conn = new PDO("sqlite:storage.db");
                                    $sql_command = <<<EOF
select * from course where (
    cid=?
)
EOF;
                                    $sql_run = $conn->prepare($sql_command);
                                    $sql_run->execute([$cid,]);
                                    $conn = null;

                                    foreach ($sql_run as $row) {
                                        $temp_cname = $row["cname"];
                                        $temp_date = $row["ctime"];
                                        $td1 = $week_name[substr($temp_date, 0, 1)];
                                        $td2 = $period_name[substr($temp_date, 2)];
                                        echo <<<EOF
<tr>
    <td>$cid</td>
    <td>$temp_cname</td>
    <td>$td1, $td2</td>
</tr>
EOF;
                                    }
                                ?>
                            </tbody>
                        </table>

                        <p class="mt-1 desc-box p-3">
                            <?php
                                $conn = new PDO("sqlite:storage.db");
                                $sql_command = <<<EOF
select * from course where (
    cid=?
)
EOF;
                                $sql_run = $conn->prepare($sql_command);
                                $sql_run->execute([$cid,]);
                                $conn = null;

                                foreach ($sql_run as $row) {
                                    echo $row["description"];
                                }
                            ?>
                        </p>

                        <button type="button" class="btn btn-outline-primary"
                                data-bs-target="#comment-modal" data-bs-toggle="modal">
                            Add my comment!
                        </button>
                        <div class="modal fade" id="comment-modal">
                            <div class="modal-dialog">
                                <form class="modal-content" method="post" action="addComment.php">
                                    <div class="modal-header">
                                        <h5 class="modal-title">Enter your comment here</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <div class="modal-body">
                                        <textarea class="form-control" name="comment-text" cols="30"
                                                  rows="5"></textarea>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-primary" name="courseID"
                                                value=<?php
                                            $temp = $_GET["courseID"];
                                            echo "$temp";
                                        ?>>Send
                                        </button>
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>

                    </div>
                </div>
                <hr>
            </div>
        </section>

        <section>
            <div class="container">
                <?php
                    $month_name = array(
                        "1" => "Jan.",
                        "2" => "Feb.",
                        "3" => "Mar.",
                        "4" => "Apr.",
                        "5" => "May",
                        "6" => "Jun.",
                        "7" => "Jul.",
                        "8" => "Aug.",
                        "9" => "Sep.",
                        "10" => "Oct.",
                        "11" => "Nov.",
                        "12" => "Dec."
                    );

                    $cid = $_GET["courseID"];
                    $conn = new PDO("sqlite:storage.db");
                    $sql_command = <<<EOF
select * from comment 
    where cid=?
    order by mdate desc, mindex asc
EOF;
                    $sql_run = $conn->prepare($sql_command);
                    $sql_run->execute([$cid,]);

                    $old_date = "";
                    $no_result = true;
                    foreach ($sql_run as $row) {
                        $no_result = false;
                        if ($row["mdate"] <> $old_date) {
                            // create a new row
                            $old_date = $row["mdate"];
                            $sql_command = <<<EOF
select name from user where sid=?
EOF;
                            $sql_run = $conn->prepare($sql_command);
                            $sql_run->execute([$row["sid"],]);
                            $name = $sql_run->fetch()["name"];
                            $text = $row["text"];
                            echo <<<EOF
<div id="$old_date" class="row my-2">
    <div class="card">
        <h5 class="card-title pt-2">$name</h5>
        <span class="card-text py-1 ps-4">$text
EOF;
                        } else {
                            // continue to add text
                            echo $row["text"];
                        }
                        if ($row["mindex"] === $row["mitotal"]) {
                            // $date_to_write = date("G:i:s,`m-d,`Y");
                            $date_to_write = explode(",`", $old_date);
                            $m_time = $date_to_write[0];
                            $m_date = explode("-", $date_to_write[1]);
                            $m_month = $month_name[$m_date[0]];
                            $m_day = $m_date[1];
                            $m_year = $date_to_write[2];
                            $day_out = $m_time . ', ' . $m_month . ' ' . $m_day . ', ' . $m_year;
                            echo <<<EOF
</span>
        <div class="card-text">
            <small class="text-muted">
                $day_out
            </small>
        </div>
    </div>
</div>                
EOF;
                        }
                    }

                    echo <<<EOF
<h3 class="text-center hide" id="no-result-warning">Nothing Found</h3>
EOF;


                    if ($no_result) {
                        echo <<<EOF
<script>
document.getElementById("no-result-warning").classList.remove("hide");
</script>
EOF;
                    }

                    $conn = null;
                ?>
            </div>
        </section>

        <script src="bootstrap_js.js"></script>

    </body>
</html>